import {combineReducers} from "redux";
import {
    FETCH_USER_REQUEST,
    FETCH_USER_RECEIVE,
    TOGGLE_MENU,
    SELECT_MENU_ITEM,
    FETCH_ACTION_REQUEST,
    FETCH_ACTION_RECEIVE,
    INVALIDATE_SUBJECT,
    FETCH_ACTION_REMOVED_RECEIVE,
    FETCH_USER_CLASS_SELECT,
    FETCH_USER_CLASS_SELECT_ALL,
    SET_FORM_ITEM,
    ENABLE_DELETE_BTN,
    SELECT_ALL_ITEM,
    SELECT_ITEM,
    OPEN_MODAL,
    CLOSE_MODAL,
    SET_ARGUMENT_ITEM,
    UPDATE_ITEM_FOR_LIST_BY_INDEX,
    ADD_ITEM_TO_LIST, SET_DEFAULT_USER
} from "../actionsType";
import {SUBJECT, PAGE_SUBJECT} from "../actions/subject";
import {categories, category} from "./category";
import {news_add} from "./news_add";
import {md5Url, pureUrl, URL} from "../libs/Url";

function toggle(state = {}, action) {
    switch (action.type) {
        case TOGGLE_MENU:
            return Object.assign({}, state, {collapse: !state.collapse});
        default:
            return {collapse: false};
    }
}
/***
 * 选中菜单项
 * @param state
 * @param action
 * @returns {*}
 */
function selected(state = {}, action) {
    switch (action.type) {
        case SELECT_MENU_ITEM:
            return Object.assign({}, state, {menuItem: !state.selected});
        default:
            return {menuItem: ''};
    }
}

function user_login(state = {
    user: {class: [], user: {}},
    isFetching: false
}, action) {
    switch (action.type) {
        case FETCH_USER_REQUEST:
            return Object.assign({}, state, {isFetching: true});
        case SET_DEFAULT_USER:
            return Object.assign({}, state, {user: action.user});
        case FETCH_USER_RECEIVE:
            return Object.assign({}, state, {isFetching: false, user: action.user});
        case FETCH_USER_CLASS_SELECT:
            return Object.assign({}, state, {
                user: {
                    ['class']: [
                        ...state.user.class.slice(0, action.index),
                        _.extend({}, state.user.class[action.index], {
                            checked: !state.user.class[action.index].checked
                        }),
                        ...state.user.class.slice(action.index + 1)
                    ]
                }
            });
        case FETCH_USER_CLASS_SELECT_ALL:
            return Object.assign({}, state, {
                user: {
                    ['class']: state.user.class.map(item=> {
                        item.checked = true;
                        return item;
                    })
                }
            });
        default:
            return state;
    }
}

///-------------------------
//全选，
export function pageBySubject(state = (()=> {
    var tmp = {};
    for (var pro in PAGE_SUBJECT) {
        tmp[PAGE_SUBJECT[pro]] = {
            enable_delete_btn: 0, //0 禁用删除按钮
            open: false, //默认关闭模态窗口
            form: { //表单功能

            }
        }
    }
    return tmp;
})(), action) {

    switch (action.type) {
        case SET_FORM_ITEM:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: Object.assign({}, state[action.subject], {
                        form: {
                            ...action.item
                        }
                    })
                }
            );
        case ENABLE_DELETE_BTN:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: Object.assign({},
                        ...state[action.subject],
                        {
                            enable_delete_btn: action.all ? action.num :
                                (state[action.subject].enable_delete_btn + action.num)
                        })
                }
            );
        case SET_ARGUMENT_ITEM:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: Object.assign({}, ...state[action.subject], {
                        arguments: Object.assign({}, ...state[action.subject].arguments || {}, action.query)
                    }),
                }
            );
        case OPEN_MODAL:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: Object.assign({}, state[action.subject], {
                        open: true
                    })
                }
            );
        case CLOSE_MODAL:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: Object.assign({}, state[action.subject], {
                        open: false
                    })
                }
            );
        default:
            return state;
    }
}

///-------------------------
/***
 * subject request
 * @param state
 * @param action
 * @returns {*}
 */
function subject_request(state = {
    isFetching: false,
    didInvalidate: false,

    query: {}//查询参数
}, action) {
    switch (action.type) {
        case INVALIDATE_SUBJECT:
            return Object.assign({}, state, {
                didInvalidate: true
            });
        case FETCH_ACTION_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case FETCH_ACTION_RECEIVE:
            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                lastUpdated: action.receivedAt,
                ...action.json,
                query: action.query
            });
        case FETCH_ACTION_REMOVED_RECEIVE:
            const tm = [];
            if (_.isArray(state.list)) {
                //批量删除
                if ((action.ids.indexOf(',') > -1) || (action.index == undefined)) {
                    //存在 list
                    state.list.map((item)=> {
                        !item.checked && tm.push(item);
                    });
                } else {
                    return Object.assign({}, state, {
                        list: [
                            ...state.list.slice(0, action.index),
                            ...state.list.slice(action.index + 1)
                        ]
                    });
                }
                return Object.assign({}, state, {
                    list: tm
                });
            }
            else {
                return Object.assign({}, state, {
                    isFetching: false,
                });
            }
        default:
            return state
    }
}
/***
 * fetch by subject
 * @param state
 * @param action
 * @returns {*}
 */
function fetchBySubject(state = (()=> {
    var tmp = {};
    for (var pro in md5Url) {
        tmp[md5Url[pro]] = {}
    }
    return tmp;
})(), action) {
    switch (action.type) {
        case INVALIDATE_SUBJECT: // invalidate subject
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            });
        case FETCH_ACTION_RECEIVE: // fetch receive subject
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            });
        case FETCH_ACTION_REQUEST: //fetch request subject
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            });
        case FETCH_ACTION_REMOVED_RECEIVE:
            let subject = {
                [action.subject]: subject_request(state[action.subject], action),
            };
            if (action.target) { //目标subject存在
                subject[action.target] = subject_request(state[action.target], action) //目标操作subject
            }
            return Object.assign({}, state, subject);
        case SELECT_ALL_ITEM:
            return Object.assign({}, state, {
                [action.subject]: Object.assign({},
                    state[action.subject],
                    {
                        list: state[action.subject].list.map((item)=> {
                            item.checked = action.flag;
                            return item;
                        })
                    })
            });
        // UPDATE_ITEM_FOR_LIST_BY_INDEX,
        // ADD_ITEM_TO_LIST
        case ADD_ITEM_TO_LIST:

            return Object.assign({}, state, {
                [action.subject]: Object.assign({}, state[action.subject], {
                    list: [
                        ...state[action.subject].list,
                        action.item
                    ]
                })
            });
        case UPDATE_ITEM_FOR_LIST_BY_INDEX:
            return Object.assign({}, state, {
                [action.subject]: Object.assign({}, state[action.subject], {
                    list: [
                        ...state[action.subject].list.slice(0, action.index),
                        _.extend({}, state[action.subject].list[action.index], action.item),
                        ...state[action.subject].list.slice(action.index + 1)
                    ]
                })
            });
        case SELECT_ITEM:
            return Object.assign({}, state, {
                [action.subject]: Object.assign({}, state[action.subject], {
                    list: [
                        ...state[action.subject].list.slice(0, action.index),
                        _.extend({}, state[action.subject].list[action.index], {
                            checked: action.checked
                        }),
                        ...state[action.subject].list.slice(action.index + 1)
                    ]
                })
            });
        default:
            return state;
    }
}
const rootReducer = {
    toggle,
    selected,
    categories,
    category,
    fetchBySubject,
    pageBySubject,
    news_add,
    user: user_login,//登录用户信息
};
export default rootReducer;